/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package KnightsTour.Controller;

import KnightsTour.Controller.DnD.GhostComponentAdapter;
import KnightsTour.Controller.DnD.GhostGlassPane;
import KnightsTour.Controller.DnD.GhostPictureAdapter;
import KnightsTour.Controller.DnD.GhostMotionAdapter;
import KnightsTour.*;
import KnightsTour.GUI.*;
import java.awt.event.*;
import javax.swing.*;


/**
 *
 * @author Scott Robinson
 */
public class Controller {

    private  ChessboardPanel boardPanel;
    private  ChessBoard board;
    private  ChessPiece piece;
    private final KnightImage image;
    private final Frame frame;
    GhostPictureAdapter pictureAdapter;
    private int steps;

    private int bestSoFar;
   
    public Controller ( ChessboardPanel cbp , ChessBoard cb, Frame frame) {
        this.boardPanel = cbp;
        this.board = cb;
        this.frame = frame;
        this.piece = new Knight(cb, 6, 3);
        this.image = new KnightImage();
        board.getChessBoardSquare(piece.getRow(), piece.getColumn()).setVisited(++steps);
        boardPanel.getChessBoardSquarePanel(piece.getRow(), piece.getColumn()).add(image);

//        GhostGlassPane glassPane = new GhostGlassPane();
//        frame.setGlassPane(glassPane);
//        image.addMouseListener(pictureAdapter = new GhostPictureAdapter(glassPane, "new_sale", ""));
////        frame.addMouseListener(new GhostComponentAdapter(glassPane, "knight moved"));
////        frame.addMouseMotionListener(new GhostMotionAdapter(glassPane));

     //   pictureAdapter.addGhostDropListener(listener);

//JButton button = new JButton("Picture adapted");
//label.addMouseListener(new GhostPictureAdapter(glassPane, "action_2", "image.png"));
//label.addMouseMotionListener(new GhostMotionAdapter(glassPane));

//        image.setTransferHandler(new TransferHandler("icon"));
//        image.addMouseListener(new DragMouseAdapter());

        for ( SquarePanel sp [] : boardPanel.getAllSquares()) {
            for (SquarePanel s : sp) {
                s.addMouseListener(new SquareListener (s));
            }
        }
    }

    private void reset() {
        boardPanel = frame.resetBoard();
        board = boardPanel.getChessBoard();
        piece = new Knight(board, 4, 2);
        board.getChessBoardSquare(piece.getRow(), piece.getColumn()).setVisited(++steps);
        boardPanel.getChessBoardSquarePanel(piece.getRow(), piece.getColumn()).add(image);

        for ( SquarePanel sp [] : boardPanel.getAllSquares()) {
            for (SquarePanel s : sp) {
                s.addMouseListener(new SquareListener (s));
            }
        }
    }

    public class SquareListener extends MouseAdapter {
        SquarePanel s;

        private SquareListener(SquarePanel s) {
            this.s = s;
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            int oldRow = piece.getRow();
            int oldColumn = piece.getColumn();
            if (piece.move(s.getSquare().getRow(), s.getSquare().getColumn())) {
                boardPanel.getChessBoardSquarePanel(oldRow, oldColumn).remove(image);
//                boardPanel.getChessBoardSquarePanel(oldRow, oldColumn).validate();
//                boardPanel.getChessBoardSquarePanel(oldRow, oldColumn).revalidate();

                boardPanel.getChessBoardSquarePanel(piece.getRow(), piece.getColumn()).add(image);
//                boardPanel.getChessBoardSquarePanel(piece.getRow(), piece.getColumn()).validate();
//                boardPanel.getChessBoardSquarePanel(piece.getRow(), piece.getColumn()).revalidate();

                board.getChessBoardSquare(piece.getRow(), piece.getColumn()).setVisited(++steps);
                boardPanel.getChessBoardSquarePanel(oldRow, oldColumn).setOrderVisited();

                image.repaint();
                if (!piece.hasMoreMoves()) gameOver();
                    
                }
            }

        }

    private void gameOver() {
        if (bestSoFar == 0) {
            JOptionPane.showMessageDialog(frame, String.format(
                    "You have no more valid moves.  You made %d steps.  Good job.", steps));
            bestSoFar = steps;
        }
        else if (steps == 64) {
            JOptionPane.showMessageDialog(frame,
                    "YOU WON!  YOU GOT ALL 64 SQUARES!  CONGRATULATIONS!!!");
        }
        else if (steps > bestSoFar) {
            bestSoFar = steps;
            JOptionPane.showMessageDialog(frame, String.format(
                    "You have no more valid moves, but you made %d steps which is your best yet!  Great job!", bestSoFar));
        }
        else {
            JOptionPane.showMessageDialog(frame, String.format(
                    "You have no more valid moves.  You made %d steps and you've hit %d before.  You can do better."
                    , steps, bestSoFar));

        }
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(frame, "Play again?", "Another Try?",
            JOptionPane.YES_NO_OPTION)) {
            steps = 0;
            reset();
            frame.validate();
        }
        else System.exit(0);
    }
    }

